package com.scau.ums.controller;

import com.scau.ums.model.Repairreport;
import com.scau.ums.service.imp.RepairReport.RepairReportImp;
import com.scau.ums.service.imp.repair.ManualTaskOrder;
import com.scau.ums.service.imp.repair.UserRepairReportImp;
import com.scau.ums.util.tools.RepairReportTool;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * author: 莫伟杰
 * date： 2019/8/5 17:07
 */
@Controller
public class OrderRepairTaskController {

    private ManualTaskOrder taskOrder;
    private RepairReportImp repairReportImp;
    private UserRepairReportImp userRepairReportImp;

    @Autowired
    public OrderRepairTaskController(ManualTaskOrder taskOrder, RepairReportImp repairReportImp,UserRepairReportImp userRepairReportImp) {
        this.taskOrder = taskOrder;
        this.repairReportImp = repairReportImp;
        this.userRepairReportImp = userRepairReportImp;
    }

    @RequestMapping(value = "/OrderRepairTask",method = RequestMethod.GET)
    public String showRepairTaskList(Model model)
    {
        model.addAttribute("reportTasks",repairReportImp.getAllUnOrderTaskRepairReports());
        return "showRepairTaskList";
    }


    @RequestMapping(value = "/OrderTask",method = RequestMethod.GET)
    public String orderRepairTask(Model model, @RequestParam Integer reportId)
    {
        Repairreport report = userRepairReportImp.getRepairreportById(reportId);
        model.addAttribute("repairReport",report);
        model.addAttribute("maintainers",taskOrder.getMaintainersByGroupId(RepairReportTool.findGroupByRepairType(report.getType())));
        return "orderTask";
    }

    @RequestMapping(value = "/CommitTaskOrder",method = RequestMethod.POST)
    @ResponseBody
    public String commitTaskOrder(@RequestParam String reportId,@RequestParam String maintainerId)
    {
        int repairReportId = Integer.parseInt(reportId);
        if(taskOrder.orderTaskToMaintain(maintainerId,repairReportId))return "true";
        else return "false";
    }

    @RequestMapping(value = "/cancelTask",method = RequestMethod.GET)
    public String cancelTask(Model model)
    {
        model.addAttribute("taskList",taskOrder.getAllCanCancelTasks());
        return "cancelTask";
    }

    @RequestMapping(value = "/commitCancel",method = RequestMethod.POST)
    @ResponseBody
    public String commitCancel(@RequestParam String taskOrderId)
    {
        Integer id = Integer.parseInt(taskOrderId);
        if(taskOrder.cancelTaskByTaskOrderId(id)) return "true";
        else return "false";
    }
}
